<template>
  <div class="scroll-view-container">
    <div class="left-scroll-view">
      <div
        class="left-scroll-view-item"
        :class="{ active: item.cat_id == isActive }"
        v-for="item in CategoryListData"
        :key="item.cat_id"
        @click="Switchactive(item.cat_id)"
        ref="leftRefitem"
      >
        {{ item.cat_name }}
      </div>
    </div>
    <div class="right-scroll-view" v-if="CategorychildrenData">
      <div v-for="(item, index) in CategorychildrenData" :key="index">
        <div class="cate-lv2-title">
          <h4>{{ item.cat_name }}</h4>
        </div>
        <div class="cate-lv3-list">
          <div
            class="cate-lv3-item"
            v-for="(childrenItem, childItemindex) in item.children"
            :key="childItemindex"
          >
            <img :src="childrenItem.cat_icon" />
            <div class="name">{{ childrenItem.cat_name }}</div>
          </div>
        </div>
      </div>
    </div>
    <div v-else style="flex: 1; margin: 200px auto">
      <van-empty description="暂无分类" />
    </div>
  </div>
</template>

<script>
import { getCategoryList } from '@/api/category'
export default {
  data () {
    return {
      CategoryListData: null,
      isActive: 1,
      CategorychildrenData: null
    }
  },
  mounted () {
    this.getCategoryListData()
  },
  methods: {
    async getCategoryListData () {
      const res = await getCategoryList()
      this.CategoryListData = res.data
      this.CategorychildrenData = this.CategoryListData[0].children
    },
    Switchactive (id) {
      this.CategorychildrenData = ''
      this.isActive = id
      this.CategorychildrenData = this.CategoryListData[id - 1].children
    }
  }
}
</script>

<style lang="scss" scoped>
.scroll-view-container {
  display: flex;
  height: 100%;

  .left-scroll-view {
    width: 120px;
    height: calc(100vh - 52px);
    overflow: auto;

    .left-scroll-view-item {
      line-height: 60px;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 12px;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;
        color: var(--ThemeColors);
      }
    }
  }

  // 去除滚动条
  .left-scroll-view::-webkit-scrollbar {
    width: 0 !important;
  }

  .right-scroll-view {
    height: calc(100vh - 52px);
    overflow: auto;
    flex: 1;
    .cate-lv2-title {
      font-size: 12px;
      font-weight: bold;
      padding: 0 15px;
    }
    .cate-lv3-list {
      display: flex;
      flex-wrap: wrap;

      .cate-lv3-item {
        width: 33.33%;
        margin-bottom: 10px;
        display: flex;
        flex-direction: column;
        align-items: center;
        img {
          width: 60px;
          height: 60px;
        }

        .name {
          font-size: 12px;
        }
      }
    }
  }

  h4 {
    display: block;
    margin-block-start: 1.33em;
    margin-block-end: 1.33em;
    margin-inline-start: 0px;
    margin-inline-end: 0px;
    font-weight: bold;
    text-align: left;
  }
  img {
    width: 16vw;
    height: 16vw;
  }

  // 去除滚动条
  .right-scroll-view::-webkit-scrollbar {
    width: 0 !important;
  }
}
</style>
